package com.weng.sso.sdk;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.weng.framework.core.model.ResponseData;
import com.weng.sso.core.config.SsoConfig;
import com.weng.sso.core.model.AuthReq;
import com.weng.sso.core.model.SsoUser;
import com.weng.sso.sdk.http.HttpClient;
import com.weng.sso.sdk.utils.SsoPropertiesUtil;

import java.io.IOException;

/**
 * Auth客户端
 **/
public class SsoClient {

    // sso服务端地址
    private String ssoServerUrl;

    private String appId;

    private String appSecret;

    private int requestTimeout = 60;


    public SsoClient() {
        init();
    }

    /**
     * 初始化
     */
    private void init() {
        this.ssoServerUrl = SsoPropertiesUtil.getServerUrl();
        this.appId = SsoPropertiesUtil.getAppId();
        this.appSecret = SsoPropertiesUtil.getAppSecret();

        int timeout = SsoPropertiesUtil.getRequestTimeout();
        if (timeout > 0) {
            this.requestTimeout = timeout * 1000;
        }
    }

    private String buildReqUrl(String reqUrl){
        return this.ssoServerUrl + reqUrl;
    }

    public ResponseData<SsoUser> token(String username, String password) {
        if(username!=null && !"".equalsIgnoreCase(username.trim())
            && password!=null && !"".equalsIgnoreCase(password.trim())){
            AuthReq req = new AuthReq();
            req.setUserName(username);
            req.setPassword(password);

            ResponseData result = null;
            try {
                result = executePostRequest(buildReqUrl(SsoConfig.SSO_LOGIN) , req);
                if (result.getData() != null) {
                    SsoUser ssoUser = Convert.convert(SsoUser.class, result.getData());
                    result.setData(ssoUser);
                }
            } catch (IOException e) {
                return ResponseData.failed("连接认证服务异常！" + e.getMessage());
            }
            return result;
        }else{
            return ResponseData.failed("用户名或密码不能为空！");
        }
    }


    public ResponseData<SsoUser> checkToken(String accessToken) {
        if (accessToken==null || "".equals(accessToken.trim())) {
            return ResponseData.failed("AccessToken不能为空");
        }
        AuthReq req = new AuthReq();
        req.setToken(accessToken);

        ResponseData result = null;
        try {
            result = executePostRequest(buildReqUrl(SsoConfig.SSO_LOGIN_CHECK) , req);
            if (result.getData() != null) {
                SsoUser ssoUser = Convert.convert(SsoUser.class, result.getData());
                result.setData(ssoUser);
            }
        } catch (IOException e) {
            return ResponseData.failed("连接认证服务异常！" + e.getMessage());
        }
        return result;
    }

    public ResponseData<SsoUser> userInfo(String accessToken) {
        if (accessToken==null || "".equals(accessToken.trim())) {
            return ResponseData.failed("AccessToken不能为空");
        }
        AuthReq req = new AuthReq();
        req.setToken(accessToken);

        ResponseData result = null;
        try {
            result = executePostRequest(buildReqUrl(SsoConfig.SSO_USER_INFO) , req);
            if (result.getData() != null) {
                SsoUser ssoUser = Convert.convert(SsoUser.class, result.getData());
                result.setData(ssoUser);
            }
        } catch (IOException e) {
            return ResponseData.failed("连接认证服务异常！" + e.getMessage());
        }
        return result;
    }

    public ResponseData changePwd(String accessToken, String password, String newPassword) {
        if (StrUtil.isEmpty(accessToken) || StrUtil.isEmpty(password) || StrUtil.isEmpty(newPassword)) {
            return ResponseData.failed("AccessToken、Password或NewPassword参数不能为空");
        }

        AuthReq req = new AuthReq();
        req.setToken(accessToken);
        req.setPassword(password);
        req.setNewPassword(newPassword);

        ResponseData result = null;
        try {
            result = executePostRequest(buildReqUrl(SsoConfig.SSO_CHANGE_PWD) , req);
        } catch (IOException e) {
            return ResponseData.failed("连接认证服务异常！" + e.getMessage());
        }
        return result;
    }



    public ResponseData logOut(String accessToken) {
        if (accessToken == null || "".equals(accessToken.trim())) {
            return ResponseData.failed("token不能为空");
        }
        AuthReq req = new AuthReq();
        req.setToken(accessToken);

        ResponseData result = null;
        try {
            result = executePostRequest(buildReqUrl(SsoConfig.SSO_LOGOUT) , req);
        } catch (IOException e) {
            return ResponseData.failed("连接认证服务异常！" + e.getMessage());
        }
        return result;
    }




    private  ResponseData executePostRequest(String bizUrl, AuthReq req) throws IOException {
        return this.buildHttpClient().post(bizUrl, req);
    }




    private HttpClient buildHttpClient() throws IOException {
        return new HttpClient(this.appId, this.appSecret,  this.requestTimeout);
    }



}
